/*
 * author      : 吴中勤 
 * date        : 2018/9/15
 * description : 因为字典对象目前没有写操作, 不需要分布式锁
 */

package com.deepexi.adapter.converter.sap.sapdict;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
public class SapDictService {

    @Autowired
    private SapDictDAS sapDictDAS;

    public List<SapDict> queryDictByType(SapDictType sapDictType) {
        if (null == sapDictType) return null;
        return sapDictDAS.listByType(sapDictType.dictType);
    }

    /** 根据 moby值, 查询字典, 如查询 证件类型字典, mobyValue=1的记录 (即身份证) */
    public SapDict getDictByMoby(SapDictType sapDictType, String mobyValue) {
        List<SapDict> dictList = queryDictByType(sapDictType);
        if (null == dictList || dictList.size() == 0) return null;
        for (SapDict oneItem : dictList) {
            if (Objects.equals(oneItem.getMobyValue(), mobyValue))
                return oneItem;
        }
        return null;
    }

    public String getSapValueByMoby(SapDictType sapDictType, String mobyValue) {
        SapDict dict = getDictByMoby(sapDictType, mobyValue);
        return null == dict ? null : dict.getSapValue();
    }

    /** 检测字典中 moby值 是否有效, 如检测 性别字典中 "男" 是否存在 */
    public boolean mobyValueCheck(SapDictType sapDictType, String mobyValue) {
        SapDict dict = getDictByMoby(sapDictType, mobyValue);
        return null != dict;
    }

    /** 刷新缓存 */
    public void invalidate() {
        sapDictDAS.invalidate();
    }

}
